package com.example.tree.controller;

import com.example.tree.config.CommonResult;
import com.example.tree.entity.PublishCritic;
import com.example.tree.entity.dto.PublishCriticDto;
import com.example.tree.entity.vo.PublishCriticVO;
import com.example.tree.service.PublishCriticService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "列表查询")
@RestController
@RequestMapping("/critic")
public class PublishCriticController {
    @Resource
    private PublishCriticService publishCriticService;

    @GetMapping("/queryList")
    @ApiOperation("评论列表查询")
    public CommonResult<?> queryList(@RequestParam("articleId") @ApiParam("文章id") String articleId,
                                     @RequestParam("pageNum") @ApiParam("页数") Integer pageNum) {
        PageHelper.startPage(pageNum, 30);
        PageInfo<PublishCriticVO> queryList = publishCriticService.queryList(articleId);
        if (pageNum > queryList.getPages()) return CommonResult.success(new PageInfo<>(Lists.newArrayList()));
        return CommonResult.success(queryList);
    }

    @PatchMapping("/add")
    @ApiOperation("添加列表")
    public CommonResult<?> add(@RequestBody PublishCriticDto publishCriticDto) {
        //模拟评论人信息，实际则在token中获取
//        publishCriticDto.setCriticUserId("1990188");
        publishCriticDto.setCriticUserId(publishCriticDto.getCriticUserId() + "_" + publishCriticDto.getIdentity());
//        publishCriticDto.setCriticUserName("我回复评论人名称");
        publishCriticDto.setCriticUserAvatar("http://png.upkeep51.com/tt/1.png");
        try {
            return CommonResult.success(publishCriticService.add(publishCriticDto));
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }


    @ApiOperation("我评论的列表")
    @GetMapping("/queryMyComment")
    public CommonResult<?> queryMyComment(@RequestParam("criticUserId") @ApiParam("评论人id") String criticUserId,
                                          @RequestParam("pageNum") @ApiParam("页数") Integer pageNum) {
        PageHelper.startPage(pageNum, 10);
        List<PublishCritic> criticList = publishCriticService.queryMyComment(criticUserId);
        PageInfo<PublishCritic> pageInfo = new PageInfo<>(criticList);
        if (pageNum > pageInfo.getPages()) return CommonResult.success(new PageInfo<>(Lists.newArrayList()));
        return CommonResult.success(pageInfo);
    }


}
